﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Castle.Services.Transaction;
using Meqs.Entity;
using Meqs.Entity.Views;
using Meqs.Logic.Interface;

namespace Meqs.Logic.Implement
{
    public class EvalItemsLogic : CommonLogic<EvalItems, int>, IEvalItemsLogic
    {
        private readonly ITemplatesLogic _tLogic;
        private readonly IEvalSheetsLogic _sLogic;
        private readonly IEvalRateLogic _erLogic;
        private readonly IAppraisalsLogic _apprLogic;

        public EvalItemsLogic(ITemplatesLogic _tLogic, IEvalSheetsLogic _sLogic, 
                              IEvalRateLogic _erLogic, IAppraisalsLogic _apprLogic)
        {
            this._tLogic = _tLogic;
            this._sLogic = _sLogic;
            this._erLogic = _erLogic;
            this._apprLogic = _apprLogic;
        }

        #region Implementation of ICommonLogic<EvalItems, int>

        /// <sammury>
        /// 创建对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Create(EvalItems entity)
        {
            return CreateObject(entity);
        }

        /// <sammury>
        /// 更新对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(EvalItems entity)
        {
            return UpdateObject(entity);
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="entityId"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public int Delete(int entityId, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("RID", entityId);
            ht.Add("SchoolMark", schoolMark);
            return DeleteCustomer("EvalItems.Delete", ht);
        }

        /// <summary>
        /// 删除所有对象
        /// </summary>
        /// <returns></returns>
        public int DeleteAll(string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            return DeleteCustomer("EvalItems.Delete", ht);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entityId"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public EvalItems FindById(int entityId, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("RID", entityId);
            ht.Add("SchoolMark", schoolMark);
            return FindCustomerByID<EvalItems>("EvalItems.FindById", ht);
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public int FindCount(string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            return FindCustomerByCount("EvalItems.FindCount", ht);
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public IList<EvalItems> FindAll(string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            return FindCustomer<EvalItems>("EvalItems.Find", ht);
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount(Hashtable table)
        {
            return FindObjectByCount(table);
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<EvalItems> FindAll(Hashtable table)
        {
            return FindObject(table);
        }

        #endregion

        /// <summary>
        /// 获取样表树的相关信息
        /// </summary>
        /// <returns></returns>
        IList<Hashtable> IEvalItemsLogic.GetAllStyleTableClsInfo(bool bGetSytleTableInfo, String schoolMark)
        {
            IList<Hashtable> result = null;
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            IList<EvalItems> list = this.FindAll(ht);
            if (list != null && list.Count > 0)
                result = new List<Hashtable>();

            // 填充样表类别的Hashtable
            foreach (EvalItems stcItem in list)
            {
                Hashtable stCls = new Hashtable();
                stCls.Add("ID", stcItem.RID);
                stCls.Add("name", stcItem.EvalName);
                stCls.Add("remark", stcItem.Remarks);

                if (bGetSytleTableInfo)
                {
                    ht = new Hashtable();
                    ht.Add("EvalItemID", stcItem.RID);
                    ht.Add("SchoolMark", schoolMark);

                    List<Hashtable> questionList = null;
                    IList<Templates> stList = _tLogic.FindAll(ht);
                    if (stList != null && stList.Count > 0)
                    {
                        questionList = new List<Hashtable>();
                        // 填充样表的Hashtable
                        foreach (Templates stItem in stList)
                        {
                            Hashtable st = new Hashtable();
                            st.Add("ID", stItem.RID);
                            st.Add("name", stItem.TemplateName);
                            st.Add("style", stItem.TemplateStyle);
                            st.Add("type", stItem.EvalType);
                            st.Add("level", stItem.TemplateLevel);

                            questionList.Add(st);
                        }
                    }
                    stCls.Add("templates", questionList);
                }
                else
                {
                    stCls.Add("templates", null);
                }
                result.Add(stCls);

            }

            return result;
        }
        /// <summary>
        /// 获取样表树的相关信息
        /// </summary>
        /// <returns></returns>
        IList<Hashtable> IEvalItemsLogic.GetAllStyleTableClsInfo(bool bGetSytleTableInfo, String schoolMark, int evalType)
        {
            IList<Hashtable> result = null;
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            ht.Add("Remarks", evalType.ToString());
            IList<EvalItems> list = this.FindAll(ht);
            if (list != null && list.Count > 0)
                result = new List<Hashtable>();

            // 填充样表类别的Hashtable
            foreach (EvalItems stcItem in list)
            {
                Hashtable stCls = new Hashtable();
                stCls.Add("ID", stcItem.RID);
                stCls.Add("name", stcItem.EvalName);
                stCls.Add("remark", stcItem.Remarks);

                if (bGetSytleTableInfo)
                {
                    ht = new Hashtable();
                    ht.Add("EvalItemID", stcItem.RID);
                    ht.Add("SchoolMark", schoolMark);
                    ht.Add("EvalType", evalType);

                    List<Hashtable> questionList = null;
                    IList<Templates> stList = _tLogic.FindAll(ht);
                    if (stList != null && stList.Count > 0)
                    {
                        questionList = new List<Hashtable>();
                        // 填充样表的Hashtable
                        foreach (Templates stItem in stList)
                        {
                            Hashtable st = new Hashtable();
                            st.Add("ID", stItem.RID);
                            st.Add("name", stItem.TemplateName);
                            st.Add("style", stItem.TemplateStyle);
                            st.Add("type", stItem.EvalType);
                            st.Add("level", stItem.TemplateLevel);

                            questionList.Add(st);
                        }
                    }
                    stCls.Add("templates", questionList);
                }
                else
                {
                    stCls.Add("templates", null);
                }
                result.Add(stCls);

            }

            return result;
        }


        // 删除指定样表类别 包含其下的所有样表
        [Transaction(TransactionMode.RequiresNew)]
        bool IEvalItemsLogic.DeleteStyleTableCls(int stclsId, String schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("EvalItemID", stclsId);
            ht.Add("SchoolMark", schoolMark);
            IList<Templates> list = _tLogic.FindAll(ht);

            if (!Deleteable(list, schoolMark))
                return false;
            

            foreach (Templates stItem in list)
            {
                bool r = _tLogic.DeleteStyleTable(Convert.ToInt32(stItem.RID), schoolMark);
                
            }
            _erLogic.DeleteRateByEIID(stclsId, schoolMark);

            _apprLogic.DeleteApprByEIID(stclsId, schoolMark);

            return this.Delete(stclsId, schoolMark) > 0 ? true : false;
        }

        /// <summary>
        /// 判断此样表类别下的所有样表能否被删除
        /// </summary>
        private bool Deleteable(IList<Templates> stList, String schoolMark)
        {
            Hashtable ht = null;
            foreach (Templates temp in stList)
            {
                ht = new Hashtable();
                ht.Add("SchoolMark", schoolMark);
                ht.Add("TemplateID", temp.RID);
                IList<EvalSheets> siList = _sLogic.FindAll(ht);
                if(siList.Count > 0)
                    return false;
            }
            return true;
        }

        /// <summary>
        /// 查询多个EvalItems
        /// </summary>
        /// <param name="idList"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public IList<EvalItems> FindEvalItemsByIdList(string idList, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            if (idList.Trim() != "")
            {
                ht.Add("TaskIDList", "(" + idList + ")");
            }
            ht.Add("SchoolMark", schoolMark);
            return FindCustomer<EvalItems>("EvalItems.FindEvalItemsByIdList", ht);
        }

        /// <summary>
        /// 查询多个EvalItems
        /// </summary>
        /// <param name="teacherID"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public IList<EvalItems> FindEvalItemsByTeacherID(int teacherID, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("TeachID", teacherID);
            ht.Add("SchoolMark", schoolMark);
            string taskIDList = FindCustomerByID<string>("EvalSettings.GetTaskIDListByTeacher", ht);
            ht.Clear();
            if (!string.IsNullOrEmpty(taskIDList))
            {
                ht.Add("TaskIDList", "(" + taskIDList.TrimEnd(',') + ")");
            }
            ht.Add("SchoolMark", schoolMark);
            return FindCustomer<EvalItems>("EvalItems.FindEvalItemsByIdList", ht);
        }
    


        /// <summary>
        /// 插入题目类别
        /// </summary>
        [Transaction(TransactionMode.RequiresNew)]
        public int CreateEvalItems(EvalItems evalItems, string schoolMark)
        {
            //try
            //{
                Hashtable ht = new Hashtable();
                ht.Add("EvalItems", evalItems);
                ht.Add("SchoolMark", schoolMark);

                int id = (int)InsertCustomer("EvalItems.InsertEvalItems", ht);

                EvalRate er = new EvalRate();
                er.RID = 0;
                er.Rate = 0;
                er.EiID = id;
                _erLogic.InsertEvalRateEx(er, schoolMark);

                IList<Appraisals> apprList = new List<Appraisals>();
                apprList.Add(new Appraisals(0, 0, 60, "差", id));
                apprList.Add(new Appraisals(0, 60, 70, "中", id));
                apprList.Add(new Appraisals(0, 70, 80, "良", id));
                apprList.Add(new Appraisals(0, 80, 100, "优", id));

                _apprLogic.SaveAppraisals(apprList, id, schoolMark);

                return id;
            //}
            //catch (Exception)
            //{
            //    return -1;
            //}
        }

        /// <summary>
        /// 更新题目类别
        /// </summary>
        public int UpdateEvalItems(EvalItems evalItems, string schoolMark)
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht.Add("EvalItems", evalItems);
                ht.Add("SchoolMark", schoolMark);

                int id = (int)UpdateCustomer("EvalItems.UpdateEvalItems", ht);
                return id;
            }
            catch (Exception)
            {
                return -1;
            }
        }

        /// <summary>
        /// 根据样表ID查询EvalItems
        /// </summary>
        /// <param name="templateID"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public EvalItems FindEvalItemsByTemplateID(int templateID, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("TemplateID", templateID);
            ht.Add("SchoolMark", schoolMark);

            return FindCustomerByID<EvalItems>("EvalItems.FindEvalItemsByTemplateID", ht);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="evalType"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public IList<EvalItems> FindEvalItemsByEvalType(EvalType evalType, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            ht.Add("Remarks", ((int)evalType).ToString());
            ht.Add("SchoolMark", schoolMark);

            return FindCustomer<EvalItems>("EvalItems.FindEvalItemsByEvalType", ht);
        }
    }

}
